﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dsjsports
{
    class SynchronizationChange : SynchronizationInfo
    {
        public SynchronizationChange(SynchronizationIObject Sender, string AdresDataaBase, string PropertyName, string NewValue, string Id)
        {
            this.sender = Sender;
            this.adres = AdresDataaBase;
            this.propertyName = PropertyName;
            this.newValue = NewValue;
            this.id = Id;
        }

        private string adres;
        public string AdresDataaBase
        {
            get { return adres; }
        }

        public string TableName
        {
            get { throw new NotImplementedException(); }
        }

        private SynchronizationIObject sender;
        public SynchronizationIObject Sender
        {
            get { return sender; }
        }

        private string propertyName;
        public string PropertyName
        {
            get { return propertyName; }
        }

        private string newValue;
        public string NewValue
        {
            get { return newValue; }
        }

        private string id;
        public string Id
        {
            get { return id; }
        }

        public bool Synchronization()
        {
            try
            {
                string updateString = "UPDATE спортсмен SET " + PropertyName + " = '" + NewValue + "' WHERE спортсмен = " + id;

                OleDbConnection DataConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + adres);
                DataConnection.Open();

                OleDbCommand SQLQuery = new OleDbCommand(updateString, DataConnection);

                int intRecordsAffected = SQLQuery.ExecuteNonQuery();

                DataConnection.Close();

                lock (sender)
                {
                    ((Sportsmen)sender).IsActual = true;
                }
                return true;
            }
            catch
            {
                ((Sportsmen)sender).IsActual = false;
                return false;
            }
        }
    }
}
